package com.dp.service.impl;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.persistence.Query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dp.service.impl3.BaseImpl;
import com.dp.util.DataMap;
import com.dp.vo.NewsClientcontrol;
import com.dp.vo.NewsPlatforminfo;

@Service
@Transactional
public class ClientcontrolServiceImpl extends BaseImpl {
	
	/**
	 * Find all the NewsClientcontrol Entity
	 * @param hashMap
	 * @param firstrow
	 * @param maxrow
	 * @return List<NewsClientcontrol>
	 */
	public List<NewsClientcontrol> findAllNewsClientcontrol(DataMap hashMap, Integer firstrow,
			Integer maxrow){
		NewsClientcontrol clientcontrol=(NewsClientcontrol) hashMap.get("clientcontrol");
		List<NewsClientcontrol> queryList=null;
		try {
			StringBuffer sf = new StringBuffer("SELECT n FROM NewsClientcontrol n WHERE 1=1 ");
			sf.append(" order by id desc");
			Query query = em.createQuery(sf.toString());
			if (firstrow != null) {
				query.setFirstResult(firstrow);
			}
			if (maxrow != null) {
				query.setMaxResults(maxrow);
			}
			queryList = query.getResultList();
		} catch (Exception e) {
			logger.error(e.getMessage());
		}
		return queryList;
	}

	/**
	 * findNewsClientcontrolById
	 * @param id
	 * @return NewsClientcontrol
	 */
	public NewsClientcontrol findNewsClientcontrolById(Integer id) {
		NewsClientcontrol o = null;
		try {
			o = this.em.find(NewsClientcontrol.class, id);
		} catch (Exception e) {
			logger.error(e.getMessage());
			e.printStackTrace();
		}
		if (o == null) {
			String msg = "Uh oh, '" + NewsClientcontrol.class + "' object with id '" + id
					+ "' not found...";
			logger.warn(msg);
			throw new EntityNotFoundException(msg);
		}
	
		return o;
	}


	/**
	 * 已选客户端平台
	 * getSelectedplatforminfoListbyid
	 * @param id
	 * @return
	 */
	public List<NewsPlatforminfo> getSelectedplatforminfoListbyid(Integer id) {
		List<NewsPlatforminfo> queryList=new ArrayList<NewsPlatforminfo>();
		try {
			//this.init();
			this.sql="SELECT platformid,mplatform,magent,mserialnum,note FROM news_platforminfo WHERE platformid in" +
					"(SELECT platformid FROM news_clientcontrolplat WHERE new_id="+id+")";
			
			NewsPlatforminfo platforminfo=null;
			List<Object[]> tempp=this.em.createNativeQuery(sql).getResultList();
			for(Object[]o:tempp)
			{
				platforminfo=new NewsPlatforminfo();
				platforminfo.setPlatformid(this.getString(o[0]));
				platforminfo.setMplatform(this.getString(o[1]));
				platforminfo.setMagent(this.getString(o[2]));
				platforminfo.setMserialnum(this.getString(o[3]));
				platforminfo.setNote(this.getString(o[4]));
				queryList.add(platforminfo);
			}
		} catch (Exception e) {
			logger.info("getSelectedplatforminfoListbyid fail !!!" + e.getMessage());
			e.printStackTrace();
		}
		return queryList;
	}
	
	/***
	 * 新增客户端控制
	 * addNewsClientcontrol 
	 * @param DataMap
	 * @return int -3:已存在一条为默认消息的记录;-2:已存在该记录;-1:操作失败;0:操作成功
	 */
	public int addNewsClientcontrol(DataMap hashMap) {
		NewsClientcontrol clientcontrol=(NewsClientcontrol) hashMap.get("clientcontrol");
		String platformids= (String)hashMap.get("platformids");
		int resultb=0;
		int id = 0;
		try {
		
			if(clientcontrol.getIsdefault()!=null){
				this.sql="SELECT count(*) FROM news_clientcontrol WHERE isdefault="+clientcontrol.getIsdefault();
				
				if(this.getOneInt()>0){
					return -3;
				}
			}
			
//			this.sql="SELECT * FROM news_clientcontrol WHERE smspf=? AND live=? AND uninstall=? AND nexturl=? AND smslive=? AND smsuninstall=? AND smsnexturl=? AND smscontentspf=? AND smssourcepf=? AND maxsms=? AND about=? AND nexttime=?";
//			this.prep=this.conn.prepareStatement(sql);	
//			prep.setString(1, clientcontrol.getSmspf());
//			prep.setString(2, clientcontrol.getLive());
//			prep.setString(3, clientcontrol.getUninstall());
//			prep.setString(4, clientcontrol.getNexturl());
//			prep.setString(5, clientcontrol.getSmslive());
//			prep.setString(6, clientcontrol.getSmsuninstall());
//			prep.setString(7, clientcontrol.getSmsnexturl());
//			prep.setString(8, clientcontrol.getSmscontentspf());
//			prep.setString(9, clientcontrol.getSmssourcepf());
//			prep.setString(10, clientcontrol.getMaxsms());
//			prep.setString(11, clientcontrol.getAbout());
//			prep.setString(12, clientcontrol.getNexttime());
//			this.rs1=prep.executeQuery();
//			if(this.rs1.next()){
//				return -2;
//			}
//			//增加客户端控制信息
//			this.sql="insert into news_clientcontrol (isdefault,smspf,live,uninstall,nexturl,smslive,smsuninstall,smsnexturl,smscontentspf,smssourcepf,maxsms,about,nexttime) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
//			this.prep=this.conn.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
//			if("".equals(platformids)){
//				prep.setInt(1, 1);	//没有关联平台 默认消息
//			}else{
//				prep.setInt(1, 2);
//			}
//			prep.setString(2, clientcontrol.getSmspf());
//			prep.setString(3, clientcontrol.getLive());
//			prep.setString(4, clientcontrol.getUninstall());
//			prep.setString(5, clientcontrol.getNexturl());
//			prep.setString(6, clientcontrol.getSmslive());
//			prep.setString(7, clientcontrol.getSmsuninstall());
//			prep.setString(8, clientcontrol.getSmsnexturl());
//			prep.setString(9, clientcontrol.getSmscontentspf());
//			prep.setString(10, clientcontrol.getSmssourcepf());
//			prep.setString(11, clientcontrol.getMaxsms());
//			prep.setString(12, clientcontrol.getAbout());
//			prep.setString(13, clientcontrol.getNexttime());
//			prep.executeUpdate();
//			this.rs1=prep.getGeneratedKeys();
//			Serializable ret = null;
//			if (rs1.next()) {
//				ret = (Serializable) rs1.getObject(1);
//				id=Integer.parseInt(ret.toString());
//			}
//			//增加规则组和客户端平台的对应关系
//			String[] selected_platform=null;
//			if(!"".equals(platformids)){
//				selected_platform=platformids.split(",");
//			}
//			if (selected_platform!= null) {
//				this.sql = "insert into news_clientcontrolplat (new_id, platformid) values (?,?)";
//				this.prep=this.conn.prepareStatement(sql);
//				for (int i = 0; i < selected_platform.length; i++) {
//					int platformid = Integer.parseInt(selected_platform [i]) ;
//					prep.setInt(1, id);
//					prep.setInt(2, platformid);
//					prep.addBatch();
//					}
//				prep.executeBatch();
//			}
//			
//			conn.commit(); 
		} catch (Exception e) {
			logger.info("addNewsClientcontrol fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=-1;
		
		}
		return resultb;
	}
	
	
	
	/**
	 * 删除客户端控制
	 * 1.//删除客户端控制和平台的关联
	 * 2.//删除客户端控制信息
	 * @param id
	 * @return	Boolean
	 */
	public Boolean deleteNewsClientcontrolById(Integer id) {
		Boolean resultb=true;
		try {
//			this.init();
			//删除客户端控制和平台的关联
			this.sql="delete from news_clientcontrolplat where new_id="+id;
			this.em.createNativeQuery(sql).executeUpdate();
			//删除客户端控制信息
			this.sql="delete from news_clientcontrol where id="+id;
			this.em.createNativeQuery(sql).executeUpdate();
		} catch (Exception e) {
			logger.info("deleteNewsClientcontrolById fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=false;
			
		}
		return resultb;
	}

	/***
	 * 更新客户端控制信息
	 * updateNewsClientcontrol 
	 * @param DataMap
	 * @return int -3:已存在一条为默认消息的记录;-2:已存在该记录;-1:操作失败;0:操作成功
	 */
	public int updateNewsClientcontrol(DataMap hashMap) {
		NewsClientcontrol clientcontrol=(NewsClientcontrol) hashMap.get("clientcontrol");
		String platformids= (String)hashMap.get("platformids");
		int resultb=0;
		try {
			
			if(clientcontrol.getIsdefault()!=null){
				this.sql="SELECT count(*) FROM news_clientcontrol WHERE isdefault="+clientcontrol.getIsdefault();
				if(this.getOneInt()>0){
					return -3;
				}
			}
//			this.sql="SELECT * FROM news_clientcontrol WHERE smspf=? AND live=? AND uninstall=? AND nexturl=? AND smslive=? AND smsuninstall=? AND smsnexturl=? AND smscontentspf=? AND smssourcepf=? AND maxsms=? AND about=? AND nexttime=? AND id!=?";
//			Query query=emr.createNativeQuery(sql);
//			query.setParameter(1, groupCatalog.getCatlogid());
//			query.executeUpdate();
//			prep.setString(1, clientcontrol.getSmspf());
//			prep.setString(2, clientcontrol.getLive());
//			prep.setString(3, clientcontrol.getUninstall());
//			prep.setString(4, clientcontrol.getNexturl());
//			prep.setString(5, clientcontrol.getSmslive());
//			prep.setString(6, clientcontrol.getSmsuninstall());
//			prep.setString(7, clientcontrol.getSmsnexturl());
//			prep.setString(8, clientcontrol.getSmscontentspf());
//			prep.setString(9, clientcontrol.getSmssourcepf());
//			prep.setString(10, clientcontrol.getMaxsms());
//			prep.setString(11, clientcontrol.getAbout());
//			prep.setString(12, clientcontrol.getNexttime());
//			prep.setInt(13, clientcontrol.getId());
//			this.rs1=prep.executeQuery();
//			if(this.rs1.next()){
//				return -2;
//			}
//			//更新客户端控制信息
//			this.sql="update news_clientcontrol set isdefault=?,smspf=?,live=?,uninstall=?,nexturl=?,smslive=?,smsuninstall=?,smsnexturl=?,smscontentspf=?,smssourcepf=?,maxsms=?,about=?,nexttime=? where id=?";
//			this.prep=this.conn.prepareStatement(sql);
//			if("".equals(platformids)){
//				prep.setInt(1, 1);	//没有关联平台 默认消息
//			}else{
//				prep.setInt(1, 2);
//			}
//			prep.setString(2, clientcontrol.getSmspf());
//			prep.setString(3, clientcontrol.getLive());
//			prep.setString(4, clientcontrol.getUninstall());
//			prep.setString(5, clientcontrol.getNexturl());
//			prep.setString(6, clientcontrol.getSmslive());
//			prep.setString(7, clientcontrol.getSmsuninstall());
//			prep.setString(8, clientcontrol.getSmsnexturl());
//			prep.setString(9, clientcontrol.getSmscontentspf());
//			prep.setString(10, clientcontrol.getSmssourcepf());
//			prep.setString(11, clientcontrol.getMaxsms());
//			prep.setString(12, clientcontrol.getAbout());
//			prep.setString(13, clientcontrol.getNexttime());
//			prep.setInt(14, clientcontrol.getId());
//			prep.executeUpdate();
//			
//			if(platformids!=null){
//				//更新客户端控制和平台的关联
//				//1.//删除客户端控制和平台的关联
//				this.sql="delete from news_clientcontrolplat where new_id=?";
//				prep=conn.prepareStatement(sql);
//				prep.setInt(1, clientcontrol.getId());
//				prep.executeUpdate();
//				//2.增加客户端控制和平台的关联
//				String[] selected_platform=null;
//				if(!"".equals(platformids)){
//					selected_platform=platformids.split(",");
//				}
//				if (selected_platform!= null) {
//					this.sql = "insert into news_clientcontrolplat (new_id, platformid) values (?,?)";
//					this.prep=this.conn.prepareStatement(sql);
//					for (int i = 0; i < selected_platform.length; i++) {
//						int platformid = Integer.parseInt(selected_platform [i]) ;
//						prep.setInt(1, clientcontrol.getId());
//						prep.setInt(2, platformid);
//						prep.addBatch();
//						}
//					prep.executeBatch();
//				}
//			}
//			
//			conn.commit(); 
		} catch (Exception e) {
			logger.info("updateNewsClientcontrol fail !!!" + e.getMessage());
			e.printStackTrace();
			resultb=-1;
			
		}
		return resultb;
	}
	
}
